package org.lyimg.com.imgapplication.presenter;

import android.util.Log;

import org.lyimg.com.imgapplication.RetrofitHelper.ImgLoader;
import org.lyimg.com.imgapplication.bean.ImgInfo;
import org.lyimg.com.imgapplication.view.IimgShowView;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * Created by liuyong on 2019/1/24.
 */

public class ImgShowPresenter extends BasePresenter<IimgShowView> {

    String url;
    ImgLoader mImgLoader = new ImgLoader();

    public ImgShowPresenter() {
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void getImgs() {

        if (null != weakReference&&!weakReference.isEnqueued()) {
            weakReference.get().showLoadingDialog();
        }

        mImgLoader.getImgShow(url).subscribe(new Consumer<List<ImgInfo>>() {
            @Override
            public void accept(List<ImgInfo> imgs) throws Exception {
                if (null != weakReference&&!weakReference.isEnqueued()) {
                    weakReference.get().dissLoadingDialog();
                    weakReference.get().showImgList(imgs);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.e("TAG", "error message:" + throwable.getMessage());
                if (null != weakReference&&!weakReference.isEnqueued()) {
                    weakReference.get().dissLoadingDialog();
                    weakReference.get().showMsgToast("请求失败");
                }
            }
        });
    }
}
